<?php
namespace app\home\controller;
use think\Db;
use think\captcha\Captcha;
use think\Loader;
class Index extends \think\Controller
{
    // public function cheshi(){
    //     $title="农夫山泉";

    //     $data=[
    //         'title'=>'php',
    //         'name'=>'农夫山泉'
    //     ];
    //     $res=Db::name('user')->field('*')->order('id desc')->limit('0,5')->select();  
    //     $this->assign('name',$title);
    //     $this->assign('data',$data);
    //     $this->assign('res',$res);
    //     return $this->fetch('cheshi');//继承基类的控制器
    // }
    public function reg(){
        // $tell=input('post.tell','','htmlspecialchars');
        // if($tell==""){
        //     $this->error("手机不能为空");
        // }
        
        // $data=[
        //     'tell'=>$tell,
        //     'code'=>input('post.code'),
        //     'pwd'=>input('post.pwd')
        // ];
        // $validate	=	Loader::validate('User');
        // if(!$validate->check($data)){
        //     $this->error($validate->getError());
        // }

        // $result=$validate->check($data);
        // if($result==false){
        //     $this->error($validate->getError());
        // }
        // $this->success('成功');
        if(isset($_POST['up'])){
            $tell=input('param.tell','','htmlspecialchars');
            $pwd=input('param.pwd','','htmlspecialchars');
            $pwd2=input('param.pwd2','','htmlspecialchars');
            $tellCode=input('param.tellCode','','htmlspecialchars');
            $picCode=input('param.picCode','','htmlspecialchars');
        $data=[
            'tell'=>$tell,
            'picCode'=>$picCode,
            'pwd'=>$pwd,
            'pwd2'=>$pwd2
        ];       //添加一条数据
        $validate	=	Loader::validate('User');
        $result=$validate->check($data);
        if($result==false){
            $this->error($validate->getError());
        }
        if($tellCode!==$_SESSION['code']){
            $this->error('短信验证错误!');
        }
        if($result==true&&$tellCode==$_SESSION['code']){
            $data=['tell'=>$_SESSION['tell'],'pwd'=>$pwd];
            $res=Db::name('user')->insertGetId($data);
            if($res>0)$this->success('注册成功!','home/index/reg');
            else $this->error('注册失败!');     
        }
        // $res=Db::name('user')->insert($data);               //执行成功返回1
        // if($res>0)$this->success('注册成功!');
        // else echo "<script>alert('注册失败!');</script>";
        }
        return $this->fetch('reg');//继承基类的控制器
    }
    public function login(){
        if(isset($_POST['put'])){
            $tell=$_POST['tell'];
            $pwd=$_POST['pwd'];
            $res=Db::name('user')->where('tell',$tell)->find();         
            if($tell==$res['tell']&&$pwd==$res['pwd']){
                echo "<script>alert('登录成功!');</script>";
            }
            else echo "<script>alert('登录失败!');</script>";
        }
        return $this->fetch('login');//继承基类的控制器
    }
    public function verification(){
        $config	=				[
            //	验证码字体大小
            'fontSize'  =>    30,				
            //	验证码位数
            'length'    =>	3,			
            //	关闭验证码杂点
            'useNoise'	=>	false,	
        ];
        $captcha	=	new	Captcha($config);
        return	$captcha->entry();
    }
    public function	check_verify($code,	$id	=''){
        $captcha	=	new	Captcha();
        return	$captcha->check($code,	$id);
    }
    public function sms(){
        // $tell=input('post.user');
        // $code=mt_rand(100000,999999);
        // Vendor('topthink.aliyun.sendSms');
        // $tell = '15625530530';
        // $ret=sendSms($tell,$code);
        // if($ret==true){
        //     echo 1;
        // }else{
        //     echo 0;
        // }
        $tell=input('param.tell','','htmlspecialchars');
        $tellCode=mt_rand(100000,999999);
        $_SESSION['code']=$tellCode;
        $_SESSION['tell']=$tell;
        Vendor('topthink.aliyun.sendSms');
        // $ret=sendSms($tell,$code);
        // if($ret==true){
        //     echo 1;
        // }else{
        //     echo 0;
        // }
        echo '成功';
    }
    public function view(){
        $ret=post('http://www.baidu.com',array());
    }
    
}
